<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>demo2</title>
</head>

<body>
  <button type="button" id="like">喜欢</button>

  <script>
    var likeBtn = document.getElementById('like')

    // 状态
    var state = {
      like: false,
      color: 'red'
    }

    // 用个函数来修改状态
    var setState = function (newState) {
      state = {
        ...state,
        ...newState
      }
      render()
    }

    // 渲染
    var render = function () {
      if (state.like) {
        // 喜欢的状态
        likeBtn.innerHTML = '已赞'
        likeBtn.style.color = 'red'
      } else {
        // 不喜欢的状态
        likeBtn.innerHTML = '喜欢'
        likeBtn.style.color = 'black'
      }
    }


    // addEventListener() 方法用于向指定元素添加事件句柄
    likeBtn.addEventListener('click', function () {
      setState(
        {
          like: !state.like,
        }
      )
      // console.log(state.like);
    })

  </script>

</body>

</html>